package com.example.demo.demo;

import com.sun.istack.internal.NotNull;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/demo2")
public class ComputerDemo {

    /**
     * 接收参数，返回结果*/
    // 允许跨域访问
    @CrossOrigin(origins = "http://localhost:5173")
    @RequestMapping(value = "/add")
    public Map<String, Integer> add(@RequestBody DataForm data) {
        // 执行逻辑
        int result = data.num1 + data.num2;
        // 封装返回值
        Map<String, Integer> ret = new HashMap<>();
        ret.put("result", result);
        return ret;
    }
}

class DataForm{
    /**
     * 该对象用于接收表单数据*/
    public Integer num1;
    public Integer num2;

    public static void main(String[] args) {
        // 包装类可以直接相加，那上述空类型，那就是没有接收到参数了
        DataForm data = new DataForm();
        data.num1 = 10;
        data.num2 = 20;
        System.out.println(data.num1 + data.num2);
    }
}

